package com.videoaggre.zcl.ui.fragment.agefans

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.RecyclerView
import com.lxj.xpopup.XPopup
import com.videoaggre.zcl.R
import com.videoaggre.zcl.bean.AgefansDetails
import com.videoaggre.zcl.bean.AgefansDetailsSerie
import com.videoaggre.zcl.common.gone
import com.videoaggre.zcl.databinding.AgefansItemSerieBinding
import com.videoaggre.zcl.ui.adapter.AgefansItemAdapter
import com.videoaggre.zcl.ui.adapter.BaseSignCheckAdapter
import com.videoaggre.zcl.ui.adapter.BaseViewHolder
import com.videoaggre.zcl.ui.dialog.SeriesPopup
import com.videoaggre.zcl.ui.fragment.BaseFragment
import com.videoaggre.zcl.ui.viewmodel.AgefansDetailsViewModel
import com.videoaggre.zcl.ui.widget.CommonButton
import com.videoaggre.zcl.utils.CenterLayoutManager
import com.videoaggre.zcl.utils.FullyGridLayoutManager
import kotlinx.android.synthetic.main.agefans_fragment_detail_intr.*

/**
 * 动漫简介
 */
class AgefansDetailIntrFragment : BaseFragment(R.layout.agefans_fragment_detail_intr) {
    private val mAgefansDetailsViewModel: AgefansDetailsViewModel by viewModels({ requireActivity() })
    private val mRecommendedAdapter: AgefansItemAdapter = AgefansItemAdapter()
    private lateinit var mAgefansDetailsSerieAdapter: AgefansDetailsSerieAdapter
    private lateinit var mAgefansDetailsPlayerListAdapter: AgefansDetailsSerieAdapter
    private var mAgefansDetails: AgefansDetails? = null
    private val mSeriesPopup: SeriesPopup by lazy {
        XPopup.Builder(context)
            .enableDrag(true)
            .asCustom(SeriesPopup(requireContext())) as SeriesPopup
    }

    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        bindViewClick(tvXuanjiMore)
        rvSeries.layoutManager = CenterLayoutManager(context, RecyclerView.HORIZONTAL, false)
        mAgefansDetailsSerieAdapter = AgefansDetailsSerieAdapter(rvSeries)
        rvSeries.adapter = mAgefansDetailsSerieAdapter
        mAgefansDetailsSerieAdapter.setOnClickAdapterListenter(::onClickAdapterSeries)
        mSeriesPopup.onClickAdapterListenter = ::postPlayer
        rvPlayerList.layoutManager = CenterLayoutManager(context, RecyclerView.HORIZONTAL, false)
        mAgefansDetailsPlayerListAdapter = AgefansDetailsSerieAdapter(rvPlayerList)
        rvPlayerList.adapter = mAgefansDetailsPlayerListAdapter
        mAgefansDetailsPlayerListAdapter.setOnClickAdapterListenter(::onClickAdapterPlayerList)
        rvRecommend.layoutManager = FullyGridLayoutManager(context, 3)
        rvRecommend.adapter = mRecommendedAdapter

    }

    override fun initData(rootView: View?, savedInstanceState: Bundle?) {
        mAgefansDetailsViewModel.mAgefansDetailsLiveData.observe(this) {
            mAgefansDetails = it
            tvIntro.changeText(it.intro)
            if (it.series.isNotEmpty()) {
                val playerList = mutableListOf<AgefansDetailsSerie>()
                for (i in 1..it.series.size) {
                    playerList.add(AgefansDetailsSerie(getString(R.string.agefans_text_play_list, i), check = 1 == i))
                }
                mAgefansDetailsPlayerListAdapter.datas = playerList
                mAgefansDetailsPlayerListAdapter.notifyDataSetChanged()
                changeSerie(it.series[0])
            }
            if (it.recommends.isEmpty()) {
                textRecommend.gone()
                rvRecommend.gone()
            } else {
                mRecommendedAdapter.datas = it.recommends
                mRecommendedAdapter.notifyDataSetChanged()
            }
        }

    }

    override fun onClick(v: View?) {
        when (v) {
            tvXuanjiMore -> mSeriesPopup.show()
        }
    }

    private fun onClickAdapterSeries(view: View, position: Int) {
        postPlayer(mAgefansDetailsSerieAdapter.getItem(position))
    }

    private fun postPlayer(agefansDetailsSerie: AgefansDetailsSerie) {
        mSeriesPopup.dismiss()
        mAgefansDetailsViewModel.mVideoPlayerLiveData.postValue(agefansDetailsSerie.link)
    }

    private fun onClickAdapterPlayerList(view: View, position: Int) {
        mAgefansDetails?.let { changeSerie(it.series[position]) }
    }

    private fun changeSerie(series: MutableList<AgefansDetailsSerie>) {
        series.forEach {
            if (it.check)
                it.check = false
        }
        mSeriesPopup.changeSerie(series)
        mAgefansDetailsSerieAdapter.datas = series
        mAgefansDetailsSerieAdapter.notifyDataSetChanged()
    }
}

class AgefansDetailsSerieAdapter(private val recyclerView: RecyclerView? = null) : BaseSignCheckAdapter<BaseViewHolder<AgefansItemSerieBinding>, AgefansDetailsSerie>() {
    override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(AgefansItemSerieBinding.inflate(getLayoutInflate(parent.context), parent, false))
    override fun onBindViewHolder(holder: BaseViewHolder<AgefansItemSerieBinding>, bean: AgefansDetailsSerie, position: Int) {
        holder.mViewBinding.run {
            bean.run {
                cbSerie.text = name
                cbSerie.changeStyle(if (check) CommonButton.CB_STROKE_NORMAL else CommonButton.CB_STROKE_TEXT)
                setViewClick(position, cbSerie)
            }
        }
    }

    override fun subOnClickAdapter(v: View, position: Int) {
        super.subOnClickAdapter(v, position)
        recyclerView?.layoutManager?.smoothScrollToPosition(recyclerView, RecyclerView.State(), position)
    }
}